import { CouponCenterType } from "../../core/enum";
import { Activity } from "../../models/activity";
import { Banner } from "../../models/banner";
import { Category } from "../../models/category";
import { spuPaging } from "../../models/spu-paging";
import { Theme } from "../../models/theme"

// pages/home/home.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        themeA: null,
        bannerB: null,
        grid: [],
        activityD: null,
        themeE: null,
        themeESpu: [],
        themeF: null,
        bannerG: null,
        themeH: null,
        spuPaging: null,
        loadingType: "loading"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        // Theme.getHomeLocat` ionA(data => {
        //     this.setData({
        //         topTheme: data[0]
        //     })
        // })
        //变成同步调用
        this.initAllData()
        this.initBottomSpuList()
    },

    async initBottomSpuList() {
        const paging = spuPaging.getLatestPaging()
        this.data.spuPaging = paging
        const data = await paging.getMoreData()

        if (!data) {
            return
        }
        //累加
        wx.lin.renderWaterFlow(data.items);
    },

    async initAllData() {
        const theme = new Theme()
        await theme.getThemes()
        const themeA = theme.getHomeLocationA()
        const themeE = theme.getHomeLocationE()
        let themeESpu = []
        if (themeE.online) {
            const data = await Theme.getHomeLocationESpu();
            if (data) {
                themeESpu = data.spu_list.slice(0, 8)
            }
        }
        const themeF = theme.getHomeLocationF()

        const bannerB = await Banner.getHomeLocationB()
        const grid = await Category.getGridCategory()
        const activityD = await Activity.getHomeLocationD()
        const bannerG = await Banner.getHomeLocationG();
        const themeH = theme.getHomeLocationH()

        // 调用方  调用过程简单的
        this.setData({
            themeA,
            bannerB,
            grid,
            activityD,
            themeE,
            themeESpu,
            themeF,
            bannerG,
            themeH
        })


    },
    onGoToCoupons(event) {
        // const name = event.target.dataset.aname

        wx.navigateTo({
            url: `/pages/coupon/coupon?name=${Activity.locationD}&type=${CouponCenterType.ACTIVITY}`,
        });
    },


    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: async function () {
        const data = await this.data.spuPaging.getMoreData();
        if (!data) {
            return
        }
        wx.lin.renderWaterFlow(data.items)
        if (!data.moreData) {
            this.setData({
                loadingType: 'end'
            })

        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})